package net.assassin8.bautista.stalker;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MallDownloadDetailsActivity extends Activity implements Runnable {
	private Stalker stalker;
	private Context ctx;
	private long mallId;
	private String mallName;
	private String address;
	private String floorNumber;
	private String description;
	
	private ProgressDialog pd;
	private Thread thread;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		stalker = ((Stalker) getApplicationContext());
		ctx = this;

		setContentView(R.layout.mall_download_details);

		TextView mallTitleTextView = (TextView) findViewById(R.id.MallTitleTextView);
		TextView addressTextView = (TextView) findViewById(R.id.AddressLabel);
		TextView floorNumberTextView = (TextView) findViewById(R.id.FloorsLabel);
		TextView descriptionTextView = (TextView) findViewById(R.id.DescriptionLabel);

		Bundle extras = getIntent().getExtras();

		mallId = extras.getLong("MALL_ID");
		mallName = extras.getString("MALL_NAME");
		address = extras.getString("MALL_ADDRESS");
		floorNumber = extras.getString("MALL_FLOORS");
		description = extras.getString("MALL_DESCRIPTION");

		mallTitleTextView.append(mallName);
		addressTextView.append(address);
		floorNumberTextView.append(floorNumber);
		descriptionTextView.append(description);

		Button downloadButton = (Button) findViewById(R.id.DownloadButton);

		downloadButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				pd = ProgressDialog.show(ctx, "Working..", "Downloading mall data...", true,
						false);

				thread = new Thread((Runnable) ctx);
				thread.start();
			}
		});
	}

	@Override
	public void run() {
		stalker.downloadMall(ctx, mallId, mallName, address,
				floorNumber, description);

		// Delete the entries in the shop categories table and insert a
		// new one
		stalker.downloadShopCategories(ctx);
		handler.sendEmptyMessage(0);
		
	}
	
	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			pd.dismiss();
			Intent i = new Intent(MallDownloadDetailsActivity.this, SelectMallActivity.class);
			MallDownloadDetailsActivity.this.startActivity(i);
			MallDownloadDetailsActivity.this.finish();
		}
	};

}
